package com.example.a2209_shop.fg

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.a2209_shop.R
import com.example.a2209_shop.adapter.CateAdapter
import com.example.a2209_shop.adapter.GoodsAdapter
import com.example.a2209_shop.databinding.FragmentHomeBinding
import com.example.a2209_shop.fgViewModel.HomeViewmodel
import com.example.base.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewmodel>() {
    val imgs = mutableListOf<String>()//存放轮播图地址的

    lateinit var cateAdapter: CateAdapter//延迟初始化分类适配器

    lateinit var goodsAdapter: GoodsAdapter
    var cateId = 14 //默认分类
    var page = 1 //默认页码
    override fun initData() {
        viewModel.bannerSuccess.observe(this){
            if(it.code==200){
                it.data.forEach {
                    imgs.add(it.imagePath)
                }
                binding.banner.setImages(imgs)
                binding.banner.setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if(context!=null&&imageView!=null){
                                Glide.with(context).load(path).into(imageView)
                            }
                        }
                    })
                binding.banner.setBannerStyle(BannerConfig.NUM_INDICATOR)//指示器样式
                binding.banner.setDelayTime(2000)//轮播时长
                binding.banner.setOnBannerListener {
                    ToastUtils.showLong("点击了$it 张图片")
                }//轮播图片点击事件
                binding.banner.start()
            }
        }

        //分类请求结果
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                //展示的是二级中的电脑办公部分
                val data = it.data[0].data
                if(data!=null){
                    cateAdapter.data.clear()//清空原来的数据
                    cateAdapter.data.addAll(data)//添加新的数据
                    cateAdapter.notifyDataSetChanged()
                }
            }
        }
        //获取商品列表请求数据
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){//当页面为初始化 或者刷新时  清空之前的数据，在添加数据
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //加入购物车后，图片发生变化----模拟点赞效果
                val goodsAdd = goodsAdapter.getViewByPosition(0,R.id.goods_add) as ImageView
                goodsAdd.setImageResource(R.drawable.ic_launcher_background)

            }
        }

    }

    override fun initView() {
        viewModel.banner()

        cateAdapter = CateAdapter()
        binding.rvCate.adapter = cateAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context,5)

        viewModel.cate()

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)

        viewModel.goods(cateId,page,10)
        //刷新框架
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.goods(cateId,page,10)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cateId,page,10)
                binding.srl.finishLoadMore()
            }
        })
        //点击分类，切换商品
        cateAdapter.setOnItemClickListener { adapter, view, position ->
            cateId = cateAdapter.data[position].id
            page=1
            viewModel.goods(cateId,page,10)
        }
        //跳转到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }
        //点击跳转到搜索界面
        binding.tvQuery.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }
        //点击加入购物车图片，实现加入购物车功能
        goodsAdapter.addChildClickViewIds(R.id.goods_add)
        goodsAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.goods_add->{viewModel.addCar(1,goodsAdapter.data[position].id)}
            }

        }


    }

    override fun getViewModel() = HomeViewmodel::class.java

    override fun getLayout(): Int  = R.layout.fragment_home
}